769d56
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
@@ -38,14 +39,14 @@
 import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
 import org.apache.hadoop.hive.metastore.MetaStoreUtils;
+import org.apache.hadoop.hive.metastore.Warehouse;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
 import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
 import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 import org.apache.hadoop.hive.metastore.api.Partition;
 import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
-import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 import org.apache.hadoop.hive.ql.metadata.HiveStorageHandler;
-import org.apache.hadoop.hive.metastore.Warehouse;
 import org.apache.hadoop.hive.ql.metadata.Table;
 import org.apache.hadoop.hive.shims.ShimLoader;
 import org.apache.hadoop.mapred.JobConf;
@@ -887,7 +888,13 @@
private void registerPartitions(JobContext context) throws IOException{
             moveTaskOutputs(fs, src, src, dest, true, table.isImmutable());
             moveTaskOutputs(fs,src,src,dest,false,table.isImmutable());
             if (!src.equals(dest)){
-              fs.delete(src, true);
+              if (src.toString().matches(".*" + Path.SEPARATOR + SCRATCH_DIR_NAME + "\\d\\.?\\d+.*")){
+                // src is scratch directory, need to trim the part key value pairs from path
+                String diff = StringUtils.difference(src.toString(), dest.toString());
+                fs.delete(new Path(StringUtils.substringBefore(src.toString(), diff)), true);
+              } else {
+                fs.delete(src, true);
+              }
             }
 
             // Now, we check if the partition already exists. If not, we go ahead.
